﻿#region Copyright information
// 
// Copyright © 2012-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the SubnetworkToolkit and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: GmtReader.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@outlook.com
// - Date Created: 2013-01-30 2:55 PM
// - Last Modified: 2013-01-30 3:21 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BOL.IO;

namespace SubnetworkToolkit.IO
{
    public class GmtReader : ReaderBase<IList<GeneSet>>
    {
        public GmtReader(Stream stream)
            : base(stream)
        {
        }

        public GmtReader(string path)
            : base(new FileStream(path, FileMode.Open))
        {
        }

        public override IList<GeneSet> Read()
        {
            var geneSets = new List<GeneSet>();

            while (!StreamReader.EndOfStream)
            {
                var line = StreamReader.ReadLine();
                var tmp = line.Split("\t".ToCharArray());
                int geneId;
                var isGeneId = Int32.TryParse(tmp[2], out geneId);

                var name = tmp[0];
                var description = tmp[1];

                var genes = isGeneId ? tmp.Skip(2).Select(x => new Gene(Int32.Parse(x))) : tmp.Skip(2).Select(x => new Gene(x));
                geneSets.Add(new GeneSet(name, description, genes));
            }

            return geneSets;
        }
    }
}